package com.itheima.advice;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

//切面
@Aspect
//被扫描
@Component
public class TimeAdvice {
    //切点
    @Pointcut("execution(* com.itheima.dao.BookDao.save(..))")
    public void pt(){}

    //通知
    @Around("TimeAdvice.pt()")
    public Object around(ProceedingJoinPoint pjp) throws Throwable {
        //前置通知-startTime
        long startTime = System.currentTimeMillis();

        //万次循环
        for (int i = 0; i < 10000; i++) {
            pjp.proceed();
        }
        //后置通知-endTime
        long endTime = System.currentTimeMillis();
        //endTime-startTime 执行时间ms
        String methodName = pjp.getSignature().getName();//获取执行方法的名称
        String className = pjp.getSignature().getDeclaringTypeName();//类路径名称
        System.out.println("执行方法"+className+"."+methodName+"万次执行效率"+(endTime - startTime)+"ms");
        return null;
    }
}
